package com.jiangyg.devtools.ui;

import com.intellij.ui.components.JBLabel;
import com.jiangyg.devtools.toolkit.Assert;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * 类描述：图标标签
 *
 * @author jiangyg
 * @date 2023-08-28
 */
public class IconLabel extends JBLabel {

    public IconLabel(String name) {
        this.setIcon(imageIcon(name));
        this.setHorizontalAlignment(CENTER);
    }

    /**
     * 功能描述：根据图标名称获取指定大小的图标
     *
     * @param name 图标名称
     * @return 图标
     */
    private ImageIcon imageIcon(String name) {
        final URL resource = IconLabel.class.getResource(String.format("/icon/%s.png", name));
        Assert.notNull(resource);
        final ImageIcon icon = new ImageIcon(resource);
        final Image image = icon.getImage().getScaledInstance(16, 16, Image.SCALE_DEFAULT);
        return new ImageIcon(image);
    }

}
